//
//  CommunicatController.m
//  NoNameAmazing
//
//  Created by liuyang on 2017/9/11.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "CommunicatController.h"
#import "HomeBodyTableViewCell.h"
#import "ReSetController.h"
@interface CommunicatController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSArray * infoArray;
@property(nonatomic,strong)NSMutableDictionary * dict;

@end

@implementation CommunicatController

- (void)viewDidLoad {
    [super viewDidLoad];
    _infoArray = @[@"电话",@"通信地址",@"电子信箱"];
    [self createTable];
    
    [self requestCommunicat];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonTitle:@"保存" image:nil frame:CGRectMake(0, 0, 30, 20) target:self sel:@selector(baoClick)];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(YDDH:) name:@"YDDH" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TXDZ:) name:@"TXDZ" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DZXX:) name:@"DZXX" object:nil];
}

-(void)createTable
{
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-44) style:UITableViewStyleGrouped];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.showsVerticalScrollIndicator=NO;
    _tableView.showsHorizontalScrollIndicator=NO;
    _tableView.userInteractionEnabled= YES ;
    
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, -5, 0);
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableView];
}

#pragma mark - 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.infoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    HomeBodyTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeBodyTableViewCell" owner:nil options:nil] lastObject];
    cell.titleLab.text = self.infoArray[indexPath.row];
    if(indexPath.row==0){
        cell.nameLab.text = _dict[@"YDDH"];
    }else if (indexPath.row==1){
        cell.nameLab.text = _dict[@"TXDZ"];
    }else if (indexPath.row==2){
        cell.nameLab.text = _dict[@"DZXX"];
    }
    return cell;
}
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 43;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0001;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray * array = @[@"YDDH",@"TXDZ",@"DZXX"];
    ReSetController *vc = [[ReSetController alloc] init];
    vc.reStr = array[indexPath.row];
    if (indexPath.row==0) {
       vc.restr = _dict[@"YDDH"];
    }else if (indexPath.row==1){
      vc.restr = _dict[@"TXDZ"];
    }else if (indexPath.row==2){
      vc.restr = _dict[@"DZXX"];
    }
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)requestCommunicat
{
    NSDictionary *params = @{@"userid":[us objectForKey:kUserid]};
    [NetWorkManager requestWithType:1 withUrlString:@"/selectInformation/getCommunication.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"message"]isEqualToString:@"成功"]) {
            _dict = [[NSMutableDictionary alloc]initWithDictionary:object[@"communication"]];
            [_tableView reloadData];
        }
        
    } withFailureBlock:^(NSError *error) {
        
        
    } progress:^(float progress) {
        
    }];

}

-(void)YDDH:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"YDDH"] = sender.userInfo[@"yddh"];
    [self.tableView reloadData];
}
-(void)TXDZ:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"TXDZ"] = sender.userInfo[@"txdz"];
    [self.tableView reloadData];
}
-(void)DZXX:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"DZXX"] = sender.userInfo[@"dzxx"];
    [self.tableView reloadData];
}

-(void)baoClick
{
    NSDictionary *params = @{@"userid":[us objectForKey:kUserid],
                             @"dzxx":_dict[@"DZXX"],
                             @"dh":_dict[@"YDDH"],
                             @"txdz":_dict[@"TXDZ"]
                             };
    [NetWorkManager requestWithType:1 withUrlString:@"/selectInformation/updateCommunication.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"status"] integerValue]==1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    } withFailureBlock:^(NSError *error) {
        
        
    } progress:^(float progress) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
